/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.imitator;

import com.tumblr.backboard.MotionProperty;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Constrains the motion between the minimum and maximum values.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public class ConstrainedMotionImitator extends MotionImitator {
    /**
     * Desired minimum spring value (overshoot may still occur).
     */
    protected double mMinValue;
    /**
     * Desired maximum spring value (overshoot may still occur).
     */
    protected double mMaxValue;

    /**
     * Constructor. Uses {@link #TRACK_ABSOLUTE} and {@link #FOLLOW_EXACT}.
     *
     * @param property the property to track.
     * @param minValue the desired minimum spring value.
     * @param maxValue the desired maximum spring value.
     */
    public ConstrainedMotionImitator(MotionProperty property, double minValue, double maxValue) {
        this(property, TRACK_ABSOLUTE, FOLLOW_EXACT, minValue, maxValue);
    }

    /**
     * Constructor.
     *
     * @param property the property to track.
     * @param trackStrategy the tracking strategy.
     * @param followStrategy the follow strategy.
     * @param minValue the desired minimum spring value.
     * @param maxValue the desired maximum spring value.
     */
    public ConstrainedMotionImitator(MotionProperty property, int trackStrategy, int followStrategy, double minValue,
            double maxValue) {
        super(property, trackStrategy, followStrategy);

        mMinValue = minValue;
        mMaxValue = maxValue;
    }

    @Override
    public void release(TouchEvent event) {
        if (mSpring != null) {
            // snap to left or right depending on current location
            if (mSpring.getCurrentValue() > mMaxValue) {
                mSpring.setEndValue(mMaxValue);
            } else if (mSpring.getCurrentValue() < mMinValue) {
                mSpring.setEndValue(mMinValue);
            }
        }
    }

    /**
     * setMinValue
     *
     * @param minValue minValue
     */
    public void setMinValue(double minValue) {
        this.mMinValue = minValue;
    }

    /**
     * setMaxValue
     *
     * @param maxValue minValue
     */
    public void setMaxValue(double maxValue) {
        this.mMaxValue = maxValue;
    }
}
